using Ant.DbServiceInterface;
using Ant.Dto.PayDto;
using Ant.EntityFrameworkCore.Model;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.Authorization;

namespace Ant.Service.Conntroller
{
    [Route("ant/{controller}")]
    public class SalesController : Controller
    {
        private readonly IPayDataService _dbService;
        private readonly IMapper _mapper;

        public SalesController(IPayDataService dbService, IMapper mapper)
        {
            this._dbService = dbService;
            this._mapper = mapper;
        }

        //创建订单
        [Route("{storeID}/{action}")]
        [HttpPost]
        [Authorize(Roles = "Admin,Clerk,Manager")]
        public IActionResult AddOrder([FromRoute] int storeID, [FromBody] CreateCommodityOrderDto createOrderDto)
        {
            var data = _mapper.Map<ICollection<CommodityOrder>>(createOrderDto.addCommodities);

            if (_dbService.CreateOrder(storeID, createOrderDto.userID, data))
            {
                return Ok();
            }
            else
            {
                return BadRequest();
            }
        }

        //获得订单
        [Route("{storeID}/{action}")]
        [HttpGet]
        [Authorize(Roles = "Admin,Clerk,Manager")]
        public async Task<IActionResult> GetOrder([FromRoute] int storeID)
        {
            var data = _mapper.Map<ICollection<GetOrderDto>>(await _dbService.GetOrder(storeID));


            if (data.Count != 0)
            {
                return Ok(_mapper.Map<ICollection<GetOrderDto>>(data));
            }
            else
            {
                return BadRequest();
            }
        }


        [Route("{storeID}/{action}")]
        [HttpGet]
        [Authorize(Roles = "Admin,Clerk,Manager")]
        public async Task<IActionResult> GetStatisticalBy24([FromRoute] int storeID, [FromQuery] DateTime start)
        {
            var xaxis = new List<string>();
            for (int i = 0; i < 24; i++)
            {
                xaxis.Add((i + 1).ToString());
            }
            return Ok(new
            {
                Data = await _dbService.GetStatisticalBy24(storeID, start),
                XAxis = xaxis
            });
        }


        [Route("{storeID}/{action}")]
        [HttpGet]
        [Authorize(Roles = "Admin,Clerk,Manager")]
        public async Task<IActionResult> GetStatisticalBy7D([FromRoute] int storeID, [FromQuery] DateTime start)
        {
            var data = await _dbService.GetStatisticalBy7D(storeID, start);
            if (start.Ticks == 0)
            {
                start = DateTime.Now;
            }

            var xaxis = new List<string>();
            start = start.AddDays(-6);
            for (int i = 0; i < 7; i++)
            {
                xaxis.Add(start.AddDays(i).Day.ToString());
            }

            return Ok(new
            {
                Data = data,
                XAxis = xaxis
            });
        }



        [Route("{storeID}/{action}")]
        [HttpGet]
        [Authorize(Roles = "Admin,Clerk,Manager")]
        public async Task<IActionResult> GetStatisticalBy1M([FromRoute] int storeID, [FromQuery] DateTime start)
        {
            var data = await _dbService.GetStatisticalBy1M(storeID, start);

            var xaxis = new List<string>();
            start = start.AddDays(start.Day - (start.Day * 2)+1);
            for (int i = 0; i < 31; i++)
            {
                if (i != 0)
                {
                    if (start.AddDays(i).Day == 1)
                    {
                        break;
                    }
                }
                xaxis.Add(start.AddDays(i).Day.ToString());
            }

            return Ok(new
            {
                Data = data,
                XAxis = xaxis
            });
        }
    }
}